Undo Command
実行したコマンドをやり直す
最後に実行したコマンドを再実行
Execute()メソッドに、isForward: bool という引数を追加して、コレがfalseなら逆再生アニメーションを再生
animation clip file や Animator Property にはRという後置詞をつけるとわかりやすい
isWalking
isWalkingR
code: InputHandler.cs
public class InputHandler : MonoBehaviour
{
...
List<Command> oldCommands = new List<Command>();
...
void HandleInput()
{
...
if (Input.GetKeyDown(KeyCode.Space))
shouldStartReplay = true;
if (Input.GetKeyDown(KeyCode.Z))
UndoLastCommand();
}
void UndoLastCommand()
{
if (oldCommands.Count > 0)
{
var lastIndex = oldCommands.Count - 1;
c.Execute(anim, false);
oldCommands.RemoveAt(lastIndex);
}
}
}
まとめ
Commandクラス
抽象クラスを作成
Execute(Animator, bool)抽象 メソッドを追加
コマンドごとに、継承して違うExecuteを実装
InputHandler クラス
oldCommands
HandleInput()
ReplayCommands()
コルーチンで順番に実行
UndoLastCommand()
最後に実行したコマンドをoldCommandsからPopして実行